import axios from 'axios' // 引入axios
import config from '@/api/config-util.js' // 引入配置文件

const instance = axios.create({
    baseURL: config.baseUrl.dev,
    timeout: 60000,
    // headers: {
    //     'xx-token': localStorage.getItem('xx-token')
    // }
})

// 请求拦截器
instance.interceptors.request.use(function (config) {
    config.headers['xx-token'] = localStorage.getItem('xx-token')
    return config
}, function (error) {
    return Promise.reject(error)
})

// 响应拦截器
instance.interceptors.response.use(function (response) {
    return response
}, function (error) {
    return Promise.reject(error)
})

// get请求
export function get (url, data = {}) {
    return new Promise((resolve, reject) => {
        instance
            .get(url, {
                params: data
            })
            .then((response) => {
                resolve(response)
            })
            .catch((err) => {
                reject(err)
            })
    })
}

// post请求
export function post (url, data = {}) {
    return new Promise((resolve, reject) => {
        instance.post(url, data).then(
            (response) => {
                resolve(response.data)
            },
            (err) => {
                reject(err)
            }
        )
    })
}
